package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.DepartmentManagement;
import com.example.prison.entity.vo.DepartmentManagementVO;
import com.example.prison.service.DepartmentManagementService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "部门管理")
@RestController
@RequestMapping("/api")
public class DepartmentManagementController {

    @Autowired
    private DepartmentManagementService departmentManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取部门列表")
    @GetMapping("/getDepart")
    public Response<PageInfo<DepartmentManagement>> getDepartment(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                  @RequestParam(value = "departName", defaultValue = "") String departName,
                                                                  @RequestParam(value = "departLevel", defaultValue = "") String departLevel) {
        PageInfo<DepartmentManagement> pageInfo = departmentManagementService.get(pageNumber, pageSize, departName, departLevel);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增部门")
    @PostMapping("/addDepart")
    public Response<?> addDepartment(@RequestBody DepartmentManagement department) {
        int result = departmentManagementService.add(department);
        if (result == -1) {
            return Response.error("部门名已存在！");
        }
        log.info("新增部门" + ", 数据：" + JSON.toJSONString(department));
        systemLogService.add("新增部门");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除部门")
    @PostMapping("/deleteDepart")
    public Response<?> deleteDepartment(@Parameter(description = "部门ID") @RequestBody Integer[] ids) {
        int result = departmentManagementService.delete(ids);
        if (result == -1) {
            return Response.error("该部门存在子部门！");
        }
        log.info("删除部门");
        systemLogService.add("删除部门");
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑部门")
    @PostMapping("/updateDepart")
    public Response<?> updateDepartment(@RequestBody DepartmentManagement department) {
        Integer id = department.getDepartId();
        int result = departmentManagementService.update(department, id);
        if (result == -1) {
            return Response.error("部门名已存在！");
        }
        log.info("编辑部门" + ", 数据：" + JSON.toJSONString(department));
        systemLogService.add("编辑部门");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取部门")
    @GetMapping("/getDepartById")
    public Response<DepartmentManagement> getDepartmentById(@Parameter(description = "部门ID") @RequestParam Integer departId) {
        DepartmentManagement department = departmentManagementService.getById(departId);
        if (ObjectUtils.isEmpty(department)) {
            return Response.error("此部门id不存在！");
        }
        return Response.success("获取成功！", department);
    }


    @ApiOperation("获取一级树状部门列表")
    @GetMapping("/getDepartTree")
    public Response<PageInfo<DepartmentManagementVO>> getDepartmentTree(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<DepartmentManagementVO> pageInfo = departmentManagementService.getTree(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }
}

